package cn.edu.hbvtc.qms.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.edu.hbvtc.qms.domain.Category;
import cn.edu.hbvtc.qms.domain.Criteria;
import cn.edu.hbvtc.qms.service.IEvaluationService;
import cn.edu.hbvtc.qms.web.struts.form.CategoryActionForm;

public class CategoryAction extends AbstractBaseDispatchAction {
	
	private IEvaluationService evaluationService = null;

	public IEvaluationService getEvaluationService() {
		return evaluationService;
	}

	public void setEvaluationService(IEvaluationService evaluationService) {
		this.evaluationService = evaluationService;
	}

	//跳转到评价项目列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		List<Category> catergoryList = evaluationService.findCategorys();
		request.setAttribute("categoryList",catergoryList);
		
		return mapping.findForward("list");
	}
	
	//跳转到评价项目添加页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		List<Criteria> criteriaList = evaluationService.findCriterias();
		request.setAttribute("criteriaList",criteriaList );
		
		return mapping.findForward("add");
	}
	
	//完成添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CategoryActionForm caf= (CategoryActionForm)form;

		if(caf.getName() == null || "".equals(caf.getName())){
			request.setAttribute("nameError","请输入模块名称！");
			
			List<Criteria> criteriaList = evaluationService.findCriterias();
			request.setAttribute("criteriaList",criteriaList );
			
			return mapping.findForward("add");
		}
		
		if(caf.getProportion() == 0 ){
			request.setAttribute("proportionError" ,"请输入比重！");
			
			List<Criteria> criteriaList = evaluationService.findCriterias();
			request.setAttribute("criteriaList",criteriaList );
			
			return mapping.findForward("add");
		}
		
		Category category = new Category();
		PropertyUtils.copyProperties(category, caf);
		
		Criteria criteria=evaluationService.findCriteriaById(caf.getCriteriaId());
		category.setCriteria(criteria);
		
		evaluationService.saveCategory(category);
		return mapping.findForward("addSave");
	}
	
	//跳转到评价项目修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CategoryActionForm caf= (CategoryActionForm)form;
		Category category = evaluationService.findCategoryById(caf.getId());
		request.setAttribute("category",category);
		
		List<Criteria> criteriaList = evaluationService.findCriterias();
		request.setAttribute("criteriaList",criteriaList );
		
		return mapping.findForward("update");
	}
	
	//完成修改功能
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CategoryActionForm caf= (CategoryActionForm)form;
		
		if(caf.getName() == null || "".equals(caf.getName())){
			request.setAttribute("nameError","请输入模块名称！");
			
			Category category = evaluationService.findCategoryById(caf.getId());
			request.setAttribute("category",category);
			
			List<Criteria> criteriaList = evaluationService.findCriterias();
			request.setAttribute("criteriaList",criteriaList );
			
			return mapping.findForward("update");
		}
		
		if(caf.getProportion() == 0 ){
			request.setAttribute("proportionError" ,"请输入比重！");
			
			Category category = evaluationService.findCategoryById(caf.getId());
			request.setAttribute("category",category);
			
			List<Criteria> criteriaList = evaluationService.findCriterias();
			request.setAttribute("criteriaList",criteriaList );
			
			return mapping.findForward("update");
		}
		
		Category category=evaluationService.findCategoryById(caf.getId());
		PropertyUtils.copyProperties(category, caf);
		
		category.setCriteria(evaluationService.findCriteriaById(caf.getCriteriaId()));
		
		evaluationService.saveCategory(category);
		
		return mapping.findForward("updateSave");
	}
	
	//完成评价项目删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CategoryActionForm caf = (CategoryActionForm)form;
		evaluationService.deleteCategoryById(caf.getId());
		
		return mapping.findForward("delete");
	}
}
